package com.ruoyi.ax.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.AuditStatus;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ax.mapper.AxMemberApplyRecordMapper;
import com.ruoyi.ax.domain.AxMemberApplyRecord;
import com.ruoyi.ax.service.IAxMemberApplyRecordService;

/**
 * 会员申请记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-19
 */
@Service
public class AxMemberApplyRecordServiceImpl implements IAxMemberApplyRecordService 
{
    @Autowired
    private AxMemberApplyRecordMapper axMemberApplyRecordMapper;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询会员申请记录
     * 
     * @param id 会员申请记录主键
     * @return 会员申请记录
     */
    @Override
    public AxMemberApplyRecord selectAxMemberApplyRecordById(Long id)
    {
        return axMemberApplyRecordMapper.selectAxMemberApplyRecordById(id);
    }

    /**
     * 查询会员申请记录列表
     * 
     * @param axMemberApplyRecord 会员申请记录
     * @return 会员申请记录
     */
    @Override
    public List<AxMemberApplyRecord> selectAxMemberApplyRecordList(AxMemberApplyRecord axMemberApplyRecord)
    {
        List<AxMemberApplyRecord> list = axMemberApplyRecordMapper.selectAxMemberApplyRecordList(axMemberApplyRecord);
        for(AxMemberApplyRecord axMemberApplyRecord1:list){
            axMemberApplyRecord1.setSysUser(userService.selectUserById(Long.valueOf(axMemberApplyRecord1.getApplyerUserid())));
        }
        return list;
    }

    /**
     * 新增会员申请记录
     * 
     * @param axMemberApplyRecord 会员申请记录
     * @return 结果
     */
    @Override
    public int insertAxMemberApplyRecord(AxMemberApplyRecord axMemberApplyRecord)
    {
        axMemberApplyRecord.setCreateTime(DateUtils.getNowDate());
        return axMemberApplyRecordMapper.insertAxMemberApplyRecord(axMemberApplyRecord);
    }


    @Override
    public int userApply(AxMemberApplyRecord axMemberApplyRecord)
    {
        AxMemberApplyRecord axMemberApplyRecordParam=new AxMemberApplyRecord();
        axMemberApplyRecordParam.setApplyerUserid(SecurityUtils.getUserId()+"");
        axMemberApplyRecordParam.setAuditStatus(1+"");
        List<AxMemberApplyRecord>  list= axMemberApplyRecordMapper.selectAxMemberApplyRecordList(axMemberApplyRecordParam);
        if(list.size()>0){
            throw new GlobalException("正在审核中，请不要重复提交");
        }

        axMemberApplyRecord.setCreateTime(DateUtils.getNowDate());
        axMemberApplyRecord.setApplyerUserid(SecurityUtils.getUserId()+"");
        return axMemberApplyRecordMapper.insertAxMemberApplyRecord(axMemberApplyRecord);
    }

    @Override
    public int userApplyAudit(AxMemberApplyRecord axMemberApplyRecord)
    {
        axMemberApplyRecord.setAuditTime(DateUtils.getNowDate());
        axMemberApplyRecord.setApplyReason(SecurityUtils.getUserId()+"");
        if(AuditStatus.审核通过.getCode().equals(axMemberApplyRecord.getAuditStatus())){
            AxMemberApplyRecord axMemberApplyRecordParam=axMemberApplyRecordMapper.selectAxMemberApplyRecordById(axMemberApplyRecord.getId());
            SysUser user=userService.selectUserById(Long.valueOf(axMemberApplyRecordParam.getApplyerUserid()));
            user.setMemberId(Long.valueOf(axMemberApplyRecordParam.getApplyMemberId()));
            userService.updateUser(user);
        }
        return axMemberApplyRecordMapper.updateAxMemberApplyRecord(axMemberApplyRecord);
    }

    /**
     * 修改会员申请记录
     * 
     * @param axMemberApplyRecord 会员申请记录
     * @return 结果
     */
    @Override
    public int updateAxMemberApplyRecord(AxMemberApplyRecord axMemberApplyRecord)
    {
        axMemberApplyRecord.setUpdateTime(DateUtils.getNowDate());
        return axMemberApplyRecordMapper.updateAxMemberApplyRecord(axMemberApplyRecord);
    }

    /**
     * 批量删除会员申请记录
     * 
     * @param ids 需要删除的会员申请记录主键
     * @return 结果
     */
    @Override
    public int deleteAxMemberApplyRecordByIds(Long[] ids)
    {
        return axMemberApplyRecordMapper.deleteAxMemberApplyRecordByIds(ids);
    }

    /**
     * 删除会员申请记录信息
     * 
     * @param id 会员申请记录主键
     * @return 结果
     */
    @Override
    public int deleteAxMemberApplyRecordById(Long id)
    {
        return axMemberApplyRecordMapper.deleteAxMemberApplyRecordById(id);
    }
}
